﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 自定义控件.Controls
{
    /// <summary>
    /// PipleLine.xaml 的交互逻辑
    /// </summary>
    public partial class PipleLine : UserControl
    {
        public PipleLine()
        {
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            // 线性发散
            LinearGradientBrush linearBrush = new LinearGradientBrush();
            linearBrush.StartPoint = new Point(0, 0);
            linearBrush.EndPoint = new Point(0, 1);
            linearBrush.GradientStops.Add(new GradientStop(Colors.Gray, 0));
            linearBrush.GradientStops.Add(new GradientStop(Colors.White, 0.5));
            linearBrush.GradientStops.Add(new GradientStop(Colors.Gray, 1));

            // 经向发散
            RadialGradientBrush radBrush = new RadialGradientBrush();
            radBrush.GradientOrigin = new Point(0.5, 0.5);
            radBrush.GradientStops.Add(new GradientStop(Colors.Gray,0));
            radBrush.GradientStops.Add(new GradientStop(Colors.White,0.5));
            radBrush.GradientStops.Add(new GradientStop(Colors.Gray,1));


            // 绘制一区弧形
            EllipseGeometry eGeometry = new EllipseGeometry(new Point(ActualHeight, ActualHeight), ActualHeight, ActualHeight);
            RectangleGeometry rGeometry = new RectangleGeometry(new Rect(x: 0, 0, ActualHeight, ActualHeight));
            CombinedGeometry comb = new CombinedGeometry(eGeometry, rGeometry);
            comb.GeometryCombineMode = GeometryCombineMode.Intersect;

            drawingContext.PushClip(comb);
            drawingContext.DrawEllipse(radBrush,null,new Point(ActualHeight,ActualHeight), ActualHeight,ActualHeight);
            drawingContext.Pop();

            // 绘制矩形区域
            drawingContext.DrawRectangle(linearBrush,null, new Rect(ActualHeight - 1, 0, ActualWidth - 2 * ActualHeight + 2, ActualHeight));

            // 绘制二区弧形
            EllipseGeometry eGeometry2 = new EllipseGeometry(new Point(ActualWidth - ActualHeight, 0), ActualHeight, ActualHeight);
            RectangleGeometry rGeometry2 = new RectangleGeometry(new Rect(ActualWidth - ActualHeight, 0, ActualHeight, ActualHeight));
            CombinedGeometry comb2 = new CombinedGeometry(eGeometry2, rGeometry2);
            comb2.GeometryCombineMode = GeometryCombineMode.Intersect;

            drawingContext.PushClip(comb2);
            drawingContext.DrawEllipse(radBrush, null, new Point(ActualWidth - ActualHeight, 0), ActualHeight, ActualHeight);
            drawingContext.Pop();

            
            Point p0 = new Point(ActualHeight / 2,ActualHeight);
            Point p1 = new Point(ActualHeight / 2, ActualHeight / 2);
            Point p2 = new Point(ActualHeight, ActualHeight / 2);
            Point p3 = new Point(ActualWidth - ActualHeight, ActualHeight / 2);
            Point p4 = new Point(ActualWidth - ActualHeight / 2, ActualHeight / 2);
            Point p5 = new Point(ActualWidth - ActualHeight / 2,0);

            PathGeometry pathGeometry = new PathGeometry();
            PathFigure pathFigure = new PathFigure();
            pathFigure.StartPoint = p0;

            BezierSegment bezier1 = new BezierSegment(p0, p1, p2,true);
            LineSegment line = new LineSegment(p3, true);
            BezierSegment bezier2 = new BezierSegment(p3, p4, p5, true);

            pathFigure.Segments.Add(bezier1);
            pathFigure.Segments.Add(line);
            pathFigure.Segments.Add(bezier2);
       
            pathGeometry.Figures.Add(pathFigure);

            Pen pen = new Pen();
            pen.Brush = new SolidColorBrush(Colors.Orange);
            pen.Thickness = 4;
            DashStyle style = new DashStyle();
            style.Dashes.Add(3);
            style.Dashes.Add(3);
            style.Offset = 0;
            pen.DashStyle = style;
            pen.DashCap = PenLineCap.Round;

            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = new Duration(TimeSpan.FromSeconds(0.4));
            animation.RepeatBehavior = RepeatBehavior.Forever;
            animation.From = 0;
            animation.To = -6;
            style.BeginAnimation(DashStyle.OffsetProperty, animation);

            drawingContext.DrawGeometry(null,pen,pathGeometry);
        }
    }
}
